<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>recursive</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        #results li.pass {
            flex: 0;
            color: green;
        }

        #results li.fail {
            flex: 0;
            color: red;
            text-decoration: line-through;
        }
    </style>
</head>
<body>
<div id="results"></div>
<script>
    function assert(value, desc) {
        let li = document.createElement('li');
        li.className = value ? 'pass' : 'fail';
        li.appendChild(document.createTextNode(desc));
        document.getElementById('results').appendChild(li);
    }


    //基础递归
    (function isPalindrome(text) {
        if (text.length <= 1) return true;
        if (text.charAt(0) != text.charAt(text.length - 1)) return false;
        return isPalindrome(text.substr(1, text.length - 2));
    })('333333');
    //    function chirp(n) {
    //        return n > 1 ? chirp(n - 1) + '-chirp' : 'chirp';
    //    }
    //    assert(chirp(3) == 'chirp-chirp-chirp', 'Calling the named function comes naturally.');
    //
    //    let ninja = {
    //        chirp: function (n) {
    //            return n > 1 ? ninja.chirp(n - 1) + '-chirp' : 'chirp';
    //        }
    //    };
    //    assert(ninja.chirp(3) === 'chirp-chirp-chirp', "An object property isn't too confusing, either.");


    //解决引用丢失--------若ninja丢失,samurai仍可取的匿名函数.
    //    var ninja = {
    //        chirp: function (n) {
    //            return n > 1 ? this.chirp(n - 1) + '-chirp' : 'chirp';
    //        }
    //    };
    //
    //    var samurai = {chirp: ninja.chirp};
    //
    //    ninja = {};
    //    try {
    //        assert(samurai.chirp(3) === 'chirp-chirp-chirp', 'Is this going to work?')
    //    }
    //    catch (e) {
    //        assert(false, "Uh, this isn't good! Where'd ninja.chirp go?")
    //    }

    //不同方式下函数name属性表现
    //笔记:若将匿名函数赋予变量,则变量名为函数名,若将匿名函数赋予方法,则.name方式取不到父对象名称
    //    这样理解有偏差,但权且为之.
    let x = {};
    x.y = function () {
    };
    let p = {
        q: function () {
        }
    };
    console.log(x.y.name);  // ''
    console.log(p.q.name);  // q
    console.log(x.y.name === "") //true

    //内联函数递归
    //    var ninja = {
    //        chirp: function signal(n) {
    //            return n > 1 ? signal(n - 1) + '-chirp' : 'chirp';
    //        }
    //    };
    //
    //    assert(ninja.chirp(3) === 'chirp-chirp-chirp', 'Works as wo would expect it to!')
    //
    //    var samurai = {chirp: ninja.chirp};
    //
    //    ninja = {};
    //
    //    assert(samurai.chirp(3) === 'chirp-chirp-chirp', 'The method correctly calls itself.')

    //内敛函数标识验证-----内敛函数命名后,函数名称只有内部可见!作用域仅限于声明他们的函数.
    var ninja = function myNinja() {
        assert(ninja === myNinja, 'This function is named two things at once!');
    };

    ninja();

    assert(typeof myNinja === 'undefined', "But myNinja ins't defined outside of the function");

</script>
</body>
</html>